//
//  SearchCollectionViewController.m
//  YiQiTingShu
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "SearchCollectionViewController.h"
#import "PublicHeader.h"
#import "FCollectionReusableView.h"
#import "ResultViewController.h"

@interface SearchCollectionViewController ()<UICollectionViewDelegateFlowLayout>
@property(nonatomic, assign)BOOL isHistory;
@property(nonatomic, strong)NSMutableArray *historyArray;
@property(nonatomic, strong)NSMutableArray *hotArray;
@end
static NSString * const header = @"Header";
static NSString * const reuseIdentifier = @"Cell";
@implementation SearchCollectionViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView registerClass:[WHNCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    [self.collectionView registerClass:[FCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:header];
    
    // Do any additional setup after loading the view.
}

-(void)initData
{
    _hotArray = [NSMutableArray array];
    
    [AFTools getDataByURLString:@"http://mobile.ximalaya.com/m/hot_search_keys?device=iPhone" WithDataBlock:^(id data) {
        for (NSString *each in [data objectForKey:@"keys"]) {
            [_hotArray addObject:each];
            
        }
        NSLog(@"%@", _hotArray);
    }];
}

//-(void)loadView
//{
//    [super loadView];
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    if (_isHistory) {
        return 2;
    }
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (_isHistory) {
        if (section == 0) {
//            return _historyArray;
            return 1;
        }
    }
    return 9;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WHNCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.layer.cornerRadius = 5;
    cell.kind = WHNCollectionViewKindOneLabel;
    if (_isHistory) {
        if (indexPath.section == 0) {
            cell.titleLabel.text = @"历史";
            return cell;
        }
    }
    
    cell.titleLabel.text = @"热门";
    // Configure the cell
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(FitConstant_XorWidth(110), FitConstant_YorHeight(30));
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.frame.size.width, 40);
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    FCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:header forIndexPath:indexPath];
    view.type = FCollectionReusableViewTypesSearch;
    if (_isHistory) {
        if (indexPath.section == 0) {
            view.titleLabel.text = @"历史搜索";
            return view;
        }
    }
    view.titleLabel.text = @"热门搜索";
    return view;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ResultViewController *vc = [[ResultViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    NSLog(@"%ld", indexPath.row);
}


#pragma mark <UICollectionViewDelegate>

/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
